package com.starblink.imgsearch.barcode.ui

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import androidx.core.view.updateLayoutParams
import com.starblink.android.basic.base.fragment.BaseTVMFragment
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.basic.ext.appScreenHeight
import com.starblink.basic.util.CommUtils
import com.starblink.imgsearch.databinding.FragmentBarCodeBinding

/**
 *
 */
class BarCodeFragment : BaseTVMFragment<FragmentBarCodeBinding, BarCodeVM>() {
    companion object {
        private const val TAG = "BarCodeFragment"
        fun obtainFragment(): BarCodeFragment {
            return BarCodeFragment()
        }
    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ): FragmentBarCodeBinding {
        return FragmentBarCodeBinding.inflate(inflater, container, false)
    }

    private var scanAnimator: ObjectAnimator? = null

    /**
     * 初始化数据
     */
    override fun initData() {
//     viewModel.initRefresh()
    }

    override fun initView(view: View) {
        super.initView(view)

        viewBinding.imgAnim.updateLayoutParams<ViewGroup.MarginLayoutParams> {
            topMargin = appScreenHeight / 8
        }
        scanAnimator = ObjectAnimator.ofFloat(
            viewBinding.imgAnim,
            "translationY",
            0f,
            appScreenHeight.shr(1).toFloat() + 120.dp2px(),
        ).apply {
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            duration = 2500L
            interpolator = AccelerateDecelerateInterpolator()
        }
    }

    override fun startObserve() {
    }

    fun starAnim() {
//        if (scanAnimator?.isStarted == false) {
//            CommUtils.getHandler().postDelayed({
//                scanAnimator?.start()
//            }, 500)
//        }
        scanAnimator?.start()
    }

    override fun onResume() {
        super.onResume()
        scanAnimator?.resume()
    }

    override fun onPause() {
        super.onPause()
        scanAnimator?.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        scanAnimator?.cancel()
    }
}